<template>
  <div>
    <!-- <h2>商品详情 {{ $route.query.id }}</h2> -->
    <van-swipe
      class="my-swipe"
      :autoplay="3000"
      indicator-color="white"
    >
      <van-swipe-item
        v-for="item in gallery"
        :key="item.id"
      ><img
          :src="item.img_url"
          alt=""
          width="100%"
          style="display:block"
        ></van-swipe-item>
    </van-swipe>
    <Tips></Tips>
    <div class="info">
      <div class="big_title">{{ info.name }}</div>
      <div class="goods_brief">{{ info.goods_brief }}</div>
      <div class="retail_price">{{ info.retail_price | RMBformat }}</div>
    </div>
    <van-cell
      title="展示弹出层"
      is-link
      @click="isSkuShow = !isSkuShow"
    />
    <div class="attribute">
      <h2>商品参数</h2>
      <ul>
        <li
          v-for="(item,index) in attribute"
          :key="index"
        >
          <div class="l">{{ item.name }}</div>
          <div class="r van-ellipsis">{{ item.value }}</div>
        </li>
      </ul>
    </div>
    <div
      class="goods_desc"
      v-html="goods_desc"
    ></div>
    <div class="title">
      <span>常见问题</span>
    </div>
    <div class="issue">
      <ul>
        <li
          v-for="item in issue"
          :key="item.id"
        >
          <div class="question">{{ item.question }}</div>
          <div class="answer">{{ item.answer }}</div>
        </li>
      </ul>
    </div>
    <div class="title">
      <span>大家都在看</span>
    </div>
    <Products :goodsList="goodsList"></Products>
    <van-sku
      ref="sku"
      v-model="isSkuShow"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />
    <AppGoodsAction
      :goodsCount="goodsCount"
      @goToCart='goToCart'
    ></AppGoodsAction>
  </div>
</template>

<script>
import Tips from "@/components/Tips.vue";
import {
  GetGoodsDetailData,
  GetGoodsRelatedData,
  GetGoodsCount,
  AddProductToCart,
} from "@/request/api.js";
import AppGoodsAction from "@/components/AppGoodsAction.vue";
import Products from "@/components/Products.vue";
export default {
  data() {
    return {
      // 轮播图数据
      gallery: [],
      // 产品信息
      info: {},
      // 产品参数
      attribute: [],
      // 商品信息 返回html
      goods_desc: "",
      // 产品的常见问题
      issue: [],
      // 相关产品
      goodsList: [],
      // 购物车产品数量
      goodsCount: 0,
      // productList
      productList: [],

      // ----------------------------------------
      // sku的显示与隐藏
      isSkuShow: false,
      sku: {
        //
        tree: [],
        hide_stock: false,
        price: "", // 默认价格（单位元）
        stock_num: "", // 商品总库存
      },

      goods: {
        // 默认商品 sku 缩略图
        picture: "",
      },
    };
  },
  components: {
    Tips,
    AppGoodsAction,
    Products,
  },
  created() {
    // 请求这个产品的详情数据
    GetGoodsDetailData({
      id: this.$route.query.id,
    }).then((res) => {
      // console.log(res);
      // this.gallery = res.data.gallery;
      // 解构赋值
      let { gallery, info, attribute, issue, productList } = res.data;
      this.gallery = gallery;
      this.info = info;
      this.attribute = attribute;
      this.goods_desc = info.goods_desc;
      this.issue = issue;
      this.goods.picture = info.list_pic_url;
      this.sku.stock_num = info.goods_number;
      this.sku.price = info.retail_price;

      // 获取productList数据
      this.productList = productList;
    });
    // 获取请求相关商品数据 (大家都在看)
    GetGoodsRelatedData({
      id: this.$route.query.id,
    }).then((res) => {
      // console.log("res", res.data);
      this.goodsList = res.data.goodsList;
    });
    // 购物车  获取购物车产品数量
    GetGoodsCount().then((res) => {
      if (res.errno == 0) {
        // console.log("res", res);
        this.goodsCount = res.data.cartTotal.goodsCount;
      }
    });
  },
  methods: {
    goToCart() {
      // 如果sku 没有展示的情况下 点击 加入购物车 展示sku
      // 如果sku 已经展示出来了  点击 加入购物车 跳转到购物车
      if (this.isSkuShow) {
        // 加入购物车
        console.log("执行加入购物车操作");
        // 获取要添加购物车的数量
        // console.log(this.$refs.sku.getSkuData().selectedNum);
        // AddProductToCart 发起加入购物车请求
        AddProductToCart({
          goodsId: this.$route.query.id,
          productId: this.productList[0].id,
          number: this.$refs.sku.getSkuData().selectedNum,
        }).then((res) => {
          if (res.errno == 0) {
            console.log("res", res);
            // 添加到购物车后做两件事情
            // 1 更新购物车数量
            this.goodsCount = res.data.cartTotal.checkedGoodsCount;
            // 2 跳转到购物车
            this.$toast.success("加入购物车成功");
            setTimeout(() => {
              this.$router.push("/cart");
            }, 1500);
          }
        });
      } else {
        this.isSkuShow = true;
      }
    },
  },
};
</script>

<style lang='less' scoped>
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
  background-color: #39a9ed;
}
.info {
  background-color: #fff;
  text-align: center;
  line-height: 2rem;
  padding: 1rem 0;
  border: 1px solid #ccc;
  .big_title {
    font-size: 1rem;
  }
  .retail_price {
    color: darkred;
  }
}

/**/
.attribute {
  background-color: #fff;
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  h2 {
    font-size: 1rem;
    line-height: 3rem;
  }
  li {
    height: 3rem;
    line-height: 1.5rem;
    background-color: #efefef;
    display: flex;
    align-items: center;
    margin-bottom: 1rem;
    border-radius: 6px;
    justify-content: space-between;
    .l {
      width: 30%;
      text-align: right;
      margin-right: 1rem;
      font-size: 0.9rem;
    }
    .r {
      flex: 1;
      font-size: 1rem;
    }
  }
}
/deep/.goods_desc {
  img {
    display: block;
    width: 100%;
  }
}
.title {
  width: 100%;
  background: #fff;
  margin-top: 0.5rem;
  height: 2rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 2rem;
    line-height: 1.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
</style>